//
//  stoneBullet.m
//  TowerDefence
//
//  Created by K3 on 5/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StoneBullet.h"
#import "GameLayer.h"
#import "iCoreGUIController.h"

@implementation StoneBullet

-(id)initStoneBulletWith:(GameLayer *)_gameLayer tower:(GameObject *)_tower {
    self = [super initBulletObjectWith:_gameLayer tower:_tower];
    velocity = 0.75f;
    damage = 20; //decrees HP of enemy down to 50 unit
    [self loadBulletComponent];
    
    return self;
}

-(void)loadBulletComponent {
    spriteBody = [[CCSprite alloc]initWithSpriteFrameName:@"stoneBullet_normal0.png"];
    //[self firedTargetAnimationWithFrameName:@"stoneBullet_explosive" frameCount:9];
}

-(void)fireTarget:(CGPoint)_targetPoint {
    targetPoint = _targetPoint;
    [self moveAnimationWithFrameName:@"stoneBullet_normal" frameCount:1];
    [self moveToTarget];
}

-(void)moveToTarget {
    CCAnimation *explosiveAnimation = [self loadAnimationFromCacheByName:@"stoneBullet_explosive" withCount:9 delay:0.05f];
    CCCallFunc *firedCallback = [CCCallFunc actionWithTarget:self selector:@selector(hitTargetCallback)];
    CCCallFunc *destroyCallBack = [CCCallFunc actionWithTarget:self selector:@selector(destroySelf)];
    [spriteBody runAction:[CCSequence actions:[CCJumpTo actionWithDuration:velocity position:targetPoint height:40 jumps:1], firedCallback, [CCAnimate actionWithAnimation:explosiveAnimation restoreOriginalFrame:NO], destroyCallBack, nil]];
}

-(void)hitTargetCallback {
    [super hitTargetCallback];
    
    if(!handler.mainGUIController.isMuteSound)
        [handler.mainGUIController playEffect:AUDIO_EXPLOSIVE];
}

-(void)update:(ccTime)_timeUpdate {
    /* if(((Enemy *)target).HP > 0) {
        if([self collision]) {
            [self hitTargetAnimationWithFrameName:@"stoneBullet_explosive" frameCount:9];
        }
    } */
}

-(void)dealloc {
    [self removeAnimationInFrameCache:@"stoneBullet_explosive"];
    
    [super dealloc];
}

@end
